
#include "replication_pad2d_tiling.h"
#include "register/op_def_registry.h"
#include "tiling/platform/platform_ascendc.h"
#include <algorithm>

namespace optiling {
        const uint32_t BLOCK_SIZE = 32;
static ge::graphStatus TilingFunc(gert::TilingContext* context)
{

  ReplicationPad2dTilingData tiling;
  uint64_t ubSize;
    auto ascendcPlatform = platform_ascendc::PlatformAscendC(context->GetPlatformInfo());
    ascendcPlatform.GetCoreMemSize(platform_ascendc::CoreMemType::UB, ubSize); //获取硬件平台存储空间 UB 的内存大小

    // //获取输入shape信息
    // uint32_t inputNum = context->GetOutputShape(0)->GetStorageShape().GetShapeSize(); //输入数量
    // uint32_t inputBytes = GetSizeByDataType(context->GetInputDesc(0)->GetDataType()); //输入类型
    // uint32_t inputLength = inputBytes * inputNum; //输入长度


    // auto dt = context->GetInputTensor(0)->GetDataType();
    // uint32_t ubDataNumber = 3*2;
    // if (dt == ge::DT_INT8) {
    //     ubDataNumber = 3*2+4;
    // }
    // else if(dt == ge::DT_INT32){
    //     ubDataNumber = 3*2+2;
    // }
    // // else {
    // //     sizeofdatatype = 4;
    // // }
    // //可使用的ub空间 输入3输出1，手动考虑双缓存
    // // uint32_t ubDataNumber = 3*2;
    // // if(inputBytes == 1)
    // //     ubDataNumber = 3*2+3*2;

    // uint32_t tileBlockNum = (ubSize / BLOCK_SIZE) / ubDataNumber; //每个ub段可用的空间块数
    // uint32_t tileDataNum = (tileBlockNum * BLOCK_SIZE) / inputBytes; //每次处理的数据量
    // uint32_t inputLengthAlgin32 = (((inputLength + BLOCK_SIZE - 1) / BLOCK_SIZE) * BLOCK_SIZE); //输入长度 对齐处理
    // uint32_t everyCoreInputBlockNum = inputLengthAlgin32 / BLOCK_SIZE;// 输入数据需要多少空间块    
    // uint32_t CoreDataNum = everyCoreInputBlockNum * BLOCK_SIZE / inputBytes; //对齐空间后的输入数量
    // uint32_t TileNum = everyCoreInputBlockNum / tileBlockNum;
    // uint32_t finalTileNum = (everyCoreInputBlockNum % tileBlockNum) == 0 ? TileNum : TileNum + 1; //需要循环处理几次
    // uint32_t TailDataNum = CoreDataNum - (tileDataNum * TileNum);
    // TailDataNum = TailDataNum == 0 ? tileDataNum : TailDataNum; //最后一次需要处理的数据量

    // tiling.set_CoreDataNum(CoreDataNum);  //对齐空间后的输入数量
    // tiling.set_finalTileNum(finalTileNum);//需要循环处理几次
    // tiling.set_tileDataNum(tileDataNum); //每次处理的数据量
    // tiling.set_TailDataNum(TailDataNum); //最后一次需要处理的数据量

    auto shape_x = context->GetInputShape(0)->GetOriginShape();
    auto shape_y = context->GetOutputShape(0)->GetOriginShape();

    int32_t x_ndarray[20];
    int32_t x_dimensional;
    int32_t size = 1;

    x_dimensional = shape_x.GetDimNum();

    for(int i = 0; i < x_dimensional; i++)
    {
        x_ndarray[i] = shape_x.GetDim(i);
        size *= x_ndarray[i];
    }

    // uint32_t ySize = context->GetOutputShape(0)->GetStorageShape().GetShapeSize();

    // context->SetTilingKey(1);

    tiling.set_size(size);
    tiling.set_x_ndarray(x_ndarray);
    tiling.set_x_dimensional(x_dimensional);
    
    context->SetBlockDim(1);
    tiling.SaveToBuffer(context->GetRawTilingData()->GetData(), context->GetRawTilingData()->GetCapacity());
    context->GetRawTilingData()->SetDataSize(tiling.GetDataSize());
    return ge::GRAPH_SUCCESS;

}
}


namespace ge {
static ge::graphStatus InferShape(gert::InferShapeContext* context)
{
    const gert::Shape* x1_shape = context->GetInputShape(0);
    gert::Shape* y_shape = context->GetOutputShape(0);
    *y_shape = *x1_shape;
    return GRAPH_SUCCESS;
}
}


namespace ops {
class ReplicationPad2d : public OpDef {
public:
    explicit ReplicationPad2d(const char* name) : OpDef(name)
    {
        this->Input("x")
            .ParamType(REQUIRED)
            .DataType({ge::DT_FLOAT, ge::DT_FLOAT16})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND});
        this->Input("paddings")
            .ParamType(REQUIRED)
            .DataType({ge::DT_INT32, ge::DT_INT32})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND});
        this->Output("y")
            .ParamType(REQUIRED)
            .DataType({ge::DT_FLOAT, ge::DT_FLOAT16})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND});

        this->SetInferShape(ge::InferShape);

        this->AICore()
            .SetTiling(optiling::TilingFunc);
        this->AICore().AddConfig("ascend310b");

    }
};

OP_ADD(ReplicationPad2d);
}
